package com.csun.nusing.ui.activity;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Window;

import com.csun.nusing.R;
import com.linktop.whealthService.HealthApi;
import com.linktop.whealthService.OnBLEService;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 随访仪的蓝牙连接界面 
 * ClassName: BleDeviceSearchActivity 
 * @Description: TODO
 * @author fzq
 * @date 2017-10-25
 */
public class BleDeviceSearchActivity extends Activity {

	private BluetoothAdapter mBtAdapter;
//	private TextView mEmptyList;
//	private Button cancelButton;
	public static final String TAG = "DeviceListActivity";
	private OnBLEService mService = null;
	// List<BluetoothDevice> deviceList;
	ArrayList<BluetoothDevice> deviceList;
	ArrayList<devicesort> devsorts;
	devicesort devsort;
//	private DeviceAdapterSort deviceAdapterSort;
	public int somevalue = 10;
	Map<String, Integer> devRssiValues;
	BluetoothDevice device;
	boolean scanning = false;
	private int time = 0;
	private Timer timer;
	private boolean refresh = true;
	private HealthApi mHealthApi;

	public static class devicesort {
		BluetoothDevice bleDevice;
		int rssi;
	}

	ComparatorUser comparator = new ComparatorUser();

	public static class ComparatorUser implements Comparator<devicesort> {

		@Override
		public int compare(devicesort lhs, devicesort rhs) {
			// TODO Auto-generated method stub
			if (lhs.rssi < rhs.rssi)
				return 1;
			else if (lhs.rssi == rhs.rssi)
				return 0;
			else
				return -1;
		}

	}

	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case OnBLEService.GATT_DEVICE_FOUND_MSG:
				Bundle data = msg.getData();
				final BluetoothDevice device = data
						.getParcelable(BluetoothDevice.EXTRA_DEVICE);
				final int rssi = data.getInt(OnBLEService.EXTRA_RSSI);
				runOnUiThread(new Runnable() {
					@Override
					public void run() {
						// if(device.getName().contains("Health"));
						addDevice(device, rssi);
					}
				});
				break;
			default:
				super.handleMessage(msg);
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
//		setContentView(R.layout.activity_ble_device_search);
		mHealthApi = new HealthApi();
		mHealthApi.initBleScan(this, mHandler);
		mHealthApi.setBleCallBack(new bleCallBacklistener());
		mBtAdapter = BluetoothAdapter.getDefaultAdapter();
//		mEmptyList = (TextView) findViewById(R.id.empty);
//		cancelButton = (Button) findViewById(R.id.btn_cancel);
		initData();
//		cancelButton.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				Log.e("", "times is " + time);
//				if (scanning) {
//					mHealthApi.bleScan(false);
//					scanning = false;
//				}
//				if (timer != null)
//					timer.cancel();
//				if (deviceAdapterSort != null)
//					deviceAdapterSort.clear();
//				finish();
//			}
//		});
	}

	private void initData() {
		/* Initialize device list container */
		Log.d(TAG, "populateList");
		deviceList = new ArrayList<BluetoothDevice>();
		devsorts = new ArrayList<devicesort>();
//		deviceAdapterSort = new DeviceAdapterSort(this, devsorts);

		devRssiValues = new HashMap<String, Integer>();

//		ListView newDevicesListView = (ListView) findViewById(R.id.new_devices);
		// newDevicesListView.setAdapter(deviceAdapter);
//		newDevicesListView.setAdapter(deviceAdapterSort);
//		newDevicesListView.setOnItemClickListener(mDeviceClickListener);

		Set<BluetoothDevice> pairedDevices = mBtAdapter.getBondedDevices();
		for (BluetoothDevice pairedDevice : pairedDevices) {
			boolean result = false;
			if (result == true) {
				addDevice(pairedDevice, 0);
			}
		}
	}

	private void addDevice(BluetoothDevice device, int rssi) {
		boolean deviceFound = false;
		// Log.e("", ""+device.getName()+" name add device ");
		for (BluetoothDevice listDev : deviceList) {
			if (listDev.getAddress().equals(device.getAddress())) {
				deviceFound = true;
				break;
			}
		}
		devRssiValues.put(device.getAddress(), rssi);
		for (devicesort d : devsorts) {
			if (d.bleDevice.getAddress().equals(device.getAddress())) {
				d.rssi = rssi;
			}
		}
		if (!deviceFound) {
//			mEmptyList.setVisibility(View.GONE);
			devsort = new devicesort();
			devsort.bleDevice = device;
			devsort.rssi = rssi;
			devsorts.add(devsort);
			Collections.sort(devsorts, comparator);

			deviceList.add(device);
			// deviceAdapter.notifyDataSetChanged();
//			deviceAdapterSort.notifyDataSetChanged();
			if (devsort.bleDevice.getName().startsWith("HC")){
				// Log.e("position", "rssi is"+d.rssi + " "+device.getAddress());
				// if (mService.isConnected(device)){
				// Log.i(TAG, "connected devcie"+device);
				// showMessage("device already connected");
				// return;
				// }
				if (scanning) {
					mHealthApi.bleScan(false);
					scanning = false;
				}
				
				Bundle b = new Bundle();
				b.putString(BluetoothDevice.EXTRA_DEVICE,
						devsort.bleDevice.getAddress());
				Intent result = new Intent();
				result.putExtras(b);
				setResult(Activity.RESULT_OK, result);
				finish();
			}
			
		} else {
			if (refresh == true) {
				refresh = false;
				time++;
				Log.i(">>>", "refresh times" + time);
				if (timer == null)
					timer = new Timer();
				timer.schedule(new TimerTask() {

					@Override
					public void run() {
						// TODO Auto-generated method stub
						refresh = true;
					}
				}, 500);
				Collections.sort(devsorts, comparator);
			}
		}

	}

	@Override
	public void onStart() {
		super.onStart();
		IntentFilter filter = new IntentFilter(
				BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
		filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
		this.registerReceiver(mReceiver, filter);
	}

	@Override
	public void onStop() {
		super.onStop();
		unregisterReceiver(mReceiver);
	}

	@Override
	protected void onDestroy() {
		mHealthApi.recoverBleScan();
		if (scanning) {
			mHealthApi.bleScan(false);
			scanning = false;
		}
		super.onDestroy();
	}

//	private OnItemClickListener mDeviceClickListener = new OnItemClickListener() {
//		@Override
//		public void onItemClick(AdapterView<?> parent, View view, int position,
//				long id) {
//			// BluetoothDevice device = deviceList.get(position);
//			// Log.e("position", "positon is"+position);
//
//			devicesort d = devsorts.get(position);
//			BluetoothDevice device = d.bleDevice;
//			// Log.e("position", "rssi is"+d.rssi + " "+device.getAddress());
//			// if (mService.isConnected(device)){
//			// Log.i(TAG, "connected devcie"+device);
//			// showMessage("device already connected");
//			// return;
//			// }
//			if (scanning) {
//				mHealthApi.bleScan(false);
//				scanning = false;
//			}
//
//			Bundle b = new Bundle();
//			b.putString(BluetoothDevice.EXTRA_DEVICE,
//					devsorts.get(position).bleDevice.getAddress());
//
//			Intent result = new Intent();
//			result.putExtras(b);
//
//			setResult(Activity.RESULT_OK, result);
//			finish();
//		}
//	};

	/**
	 * The BroadcastReceiver that listens for discovered devices and changes the
	 * title when discovery is finished.
	 */
	private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();

			if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
				setProgressBarIndeterminateVisibility(false);
				setTitle(R.string.please_select_ble_device);
				if (deviceList.size() == 0) {
//					mEmptyList.setText("无蓝牙设备");
				}
			}
			if (BluetoothAdapter.ACTION_STATE_CHANGED
					.equals(intent.getAction())) {
				if (!mBtAdapter.isEnabled())
					finish();
			}
		}
	};

//	class DeviceAdapter extends BaseAdapter {
//		Context context;
//		List<BluetoothDevice> devices;
//		LayoutInflater inflater;
//
//		public DeviceAdapter(Context context, List<BluetoothDevice> devices) {
//			this.context = context;
//			inflater = LayoutInflater.from(context);
//			this.devices = devices;
//		}
//
//		@Override
//		public int getCount() {
//			return devices.size();
//		}
//
//		@Override
//		public Object getItem(int position) {
//			return devices.get(position);
//		}
//
//		@Override
//		public long getItemId(int position) {
//			return position;
//		}
//
//		@Override
//		public View getView(int position, View convertView, ViewGroup parent) {
//			ViewGroup vg;
//
//			if (convertView != null) {
//				vg = (ViewGroup) convertView;
//			} else {
//				vg = (ViewGroup) inflater.inflate(R.layout.devicedetail, null);
//			}
//
//			BluetoothDevice device = devices.get(position);
//			final TextView tvadd = ((TextView) vg.findViewById(R.id.address));
//			final TextView tvname = ((TextView) vg.findViewById(R.id.name));
//			final TextView tvpaired = (TextView) vg.findViewById(R.id.paired);
//			final TextView tvrssi = (TextView) vg.findViewById(R.id.rssi);
//
//			tvrssi.setVisibility(View.VISIBLE);
//			byte rssival = (byte) devRssiValues.get(device.getAddress())
//					.intValue();
//			if (rssival != 0) {
//				tvrssi.setText("Rssi = " + String.valueOf(rssival));
//			}
//
//			tvname.setText(device.getName());
//			tvadd.setText(device.getAddress());
//			if (device.getBondState() == BluetoothDevice.BOND_BONDED) {
//				tvname.setTextColor(Color.GRAY);
//				tvadd.setTextColor(Color.GRAY);
//				tvpaired.setTextColor(Color.GRAY);
//				tvpaired.setVisibility(View.VISIBLE);
//				tvpaired.setText("准备");
//				tvrssi.setVisibility(View.GONE);
//			} else {
//				tvname.setTextColor(Color.GRAY);
//				tvadd.setTextColor(Color.GRAY);
//				tvpaired.setVisibility(View.GONE);
//				tvrssi.setVisibility(View.VISIBLE);
//				tvrssi.setTextColor(Color.GRAY);
//			}
//			return vg;
//		}
//	}
//
//	class DeviceAdapterSort extends BaseAdapter {
//		Context context;
//		List<devicesort> devices;
//		LayoutInflater inflater;
//
//		public DeviceAdapterSort(Context context, List<devicesort> devices) {
//			this.context = context;
//			inflater = LayoutInflater.from(context);
//			this.devices = devices;
//		}
//
//		public void clear() {
//			devices.clear();
//		}
//
//		@Override
//		public int getCount() {
//			return devices.size();
//		}
//
//		@Override
//		public Object getItem(int position) {
//			return devices.get(position);
//		}
//
//		@Override
//		public long getItemId(int position) {
//			return position;
//		}
//
//		@Override
//		public View getView(int position, View convertView, ViewGroup parent) {
//			ViewGroup vg;
//
//			if (convertView != null) {
//				vg = (ViewGroup) convertView;
//			} else {
//				vg = (ViewGroup) inflater.inflate(R.layout.devicedetail, null);
//			}
//
//			BluetoothDevice device = devices.get(position).bleDevice;
//			final TextView tvadd = ((TextView) vg.findViewById(R.id.address));
//			final TextView tvname = ((TextView) vg.findViewById(R.id.name));
//			final TextView tvpaired = (TextView) vg.findViewById(R.id.paired);
//			final TextView tvrssi = (TextView) vg.findViewById(R.id.rssi);
//
//			tvrssi.setVisibility(View.VISIBLE);
//			// byte rssival = (byte)
//			// devRssiValues.get(device.getAddress()).intValue();
//			byte rssival = (byte) devices.get(position).rssi;
//			if (rssival != 0) {
//				tvrssi.setText("Rssi = " + String.valueOf(rssival));
//			}
//
//			tvname.setText(device.getName());
//			tvadd.setText(device.getAddress());
//			if (device.getBondState() == BluetoothDevice.BOND_BONDED) {
//				tvname.setTextColor(Color.GRAY);
//				tvadd.setTextColor(Color.GRAY);
//				tvpaired.setTextColor(Color.GRAY);
//				tvpaired.setVisibility(View.VISIBLE);
//				tvpaired.setText("准备");
//				tvrssi.setVisibility(View.GONE);
//			} else {
//				tvname.setTextColor(Color.BLACK);
//				tvadd.setTextColor(Color.BLACK);
//				tvpaired.setVisibility(View.GONE);
//				tvrssi.setVisibility(View.VISIBLE);
//				tvrssi.setTextColor(Color.BLACK);
//			}
//			return vg;
//		}
//	}

	/**
	 * 蓝牙连接监听 ClassName: bleCallBacklistener
	 * 
	 * @Description: TODO
	 * @author fzq
	 * @date 2017-10-25
	 */
	private class bleCallBacklistener implements HealthApi.BleCallBack {
		@Override
		public void bleCallBack(int type, Object data) {
			// TODO Auto-generated method stub
			if (type == HealthApi.BLE_BIND_FINISHED) {
				if (scanning == false) {
					Log.e(TAG, "scanning");
					mHealthApi.bleScan(true);
					scanning = true;
				}
			}
		}
	}

}
